//<script language="javascript">
// Manages User Messages lookup and formatting for Javascript
// Copyright (C) 2003 Outtask, Inc. All Rights Reserved.

// *** Corresponds to inc_usermessage.asp ***

var UM_RAW = -1;
var UM_TEXT = 0;
var UM_HTML = 1;

var umStartParm = "\001";
var umEndParm = "\002";
var umStartRepeat = "\003";
var umEndRepeat = "\004";

// --- Language ID and array global variables ---

// Language ID - might be needed by some formatting functions
var OTLangID = "en-us";		// this will be overriden by included language JS files like
							// /javascript/usermessagetable.asp?cat=Category&lang=de

// Language array - usermessagetable.asp will add categories of messages to this
var OTLang = {};

// --- Retrieve Message --

// Use this to retrieve a user message. It will only exist if you also included the JavaScript
// message table generated by userMessageTable.asp - you can do this automatically using the
// page object by specifying page.RequireMessages("Category")
function OTGetUserMessage(category, id)
{
	var result = null;
	if (OTLang != null)
	{
		var cat = OTLang[category.toLowerCase()];
		if (cat != null && id != null && id.toLowerCase)
			result = cat[id.toLowerCase()];
	}
	if (result == null)
		result = id;
	return result;
}

// --- Message Formatting ---

// internal
function umGetParmCount(parmsDict, name)
{
	var v, result;
	if (typeof(parmsDict) == "object")
		v = parmsDict[name];
	else
	{
		if (name == "0" || name == 0)
			v = parmsDict;
		else
			v = null;
	}
	if (v == null)
		result = 0;
	else if (typeof(v) == "object")
		result = v.length;
	else
		result = 1;
	return result;
}

// internal
function umGetParm(parmsDict, name, index)
{
	var v, result;
	if (typeof(parmsDict) == "object")
		v = parmsDict[name];
	else
	{
		if (name == "0" || name == 0)
			v = parmsDict;
		else
			v = null;
	}
	if (v == null)
		v = "";
	if (typeof(v) == "object")
	{
		if (v.length > index)
			result = v[index];
		else
			result = "";
	}
	else
	{
		if (index == 0)
			result = v;
		else
			result = "";
	}
	return result;
}

// internal
function umFormatParm(lang, parm, parmsDict, index)
{
	var result = "", v, redo, args, i, gotArg, pval;
	v = parm.split(":");
	if (v.length > 1)
	{
		gotArg = false;
		redo = false;
		pval = umGetParm(parmsDict, v[0], index);
		for (i = 2; i < v.length; i++)
		{
			if (i > 2)
				args += ",";
			if (v[i] == "*")
				v[i] = lang;
			if (v[i] == ".")
			{
				v[i] = pval;
				gotArg = true;
			}
			args += "\"" + v[i] + "\"";
		}
		if (!gotArg)
			args = "\"" + pval + "\"," + args;
		try
		{
			result = eval(v[1].toLowerCase() + "(" + args + ")");
		}
		catch (e)
		{
			redo = true;
		}
		if (redo)
			result = umGetParm(parmsDict, v[0], index);
	}
	else
		result = umGetParm(parmsDict, v[0], index);
	return result;
}

// internal
function umFormatLine(lang, line, parmsDict, index)
{
	var result = "", i, phrases, parts;
	phrases = line.split(umStartParm);
	for (i = 0; i < phrases.length; i++)
	{
		parts = phrases[i].split(umEndParm);
		if (parts.length > 1)	// has a variable
			result += umFormatParm(lang, parts[0], parmsDict, index) + parts[1];
		else
			result += phrases[i];
	}
	return result;
}

// internal
function umFormatUserMessageText(lang, mode, msg, parmsDict)
{
	var result = "", i, j, c, lines, line, txt, ulIsOpen;
	ulIsOpen = false;
	lines = msg.split(umStartRepeat);
	for (i = 0; i < lines.length; i++)
	{
		line = lines[i].split(umEndRepeat);
		if (line.length > 1)	// repeat
		{
			if (line[0] == "")
				c = 1;
			else
				c = umGetParmCount(parmsDict, line[0]);
			if (c > 0 && !ulIsOpen)
			{
				if (mode == UM_HTML)
					result += "<br/><UL>";
				else if (mode == UM_TEXT)
					result += "\r\n";
				ulIsOpen = true;
			}
			for (j = 0; j < c; j++)
			{
				txt = umFormatLine(lang, line[1], parmsDict, j);
				if (mode == UM_TEXT)
				{
					if (result.substring(result.length - 1, result.length) != "\n")
						result += "\r\n";
					result += "- " + txt;
				}
				else if (mode == UM_HTML)
					result += "<LI>" + txt + "</LI>";
				else
					result += txt;
			}
		}
		else	// not repeat
		{
			txt = umFormatLine(lang, lines[i], parmsDict, 0);
			if (ulIsOpen)
			{
				if (mode == UM_HTML)
					result += "</UL>";
				else if (mode == UM_TEXT)
					result += "\r\n";
				ulIsOpen = false;
			}
			if (mode == UM_HTML)
			{
				txt = txt.replace(/\r\n/gi, "<br/>");
				txt = txt.replace(/\n/gi, "<br/>");
			}
			result += txt;
		}
	}
	if (ulIsOpen)
	{
		if (mode == UM_HTML)
			result += "</UL>";
		else if (mode == UM_TEXT)
			result += "\r\n";
		ulIsOpen = false;
	}
	return result;
}

// internal
function umFormatUserMessage(mode, category, id, args)
{
	var arr = new Array();

	if (args != null)
	{
		// TODO: need a better way to do this - can't seem to differentiate object from array
		// so, object must define _dict to indicate what you want to do
		if (args.length == 3 && args[2] != null && typeof(args[2]) == "object" && args[2]["_dict"] != null)
		{
			// one and only argument is an object
			arr = args[2];
		}
		else	// arguments were passed as array
		{
			for (var i = 2; i < args.length; i++)
				arr[i - 2] = args[i];
		}
	}

	return umFormatUserMessageText(OTLangID, mode, OTGetUserMessage(category, id), arr);
}

// Format a user message in RAW mode and get result as string.
// RAW mode gives you the exact message text.
function OTFormatUserMessage(category, id)
{
	return umFormatUserMessage(UM_RAW, category, id, arguments);
}

// Format a user message in TEXT mode and get result as string.
// TEXT mode gives you the text "nicely" formatted where repeating
// sections appear as bullet lists starting with a dash.
function OTFormatUserMessageText(category, id)
{
	return umFormatUserMessage(UM_TEXT, category, id, arguments);
}

// Format a user message in HTML mode and get result as string.
// HTML mode gives you the text "nicely" formatted where repeating
// sections appear as bullet lists using <UL>.
function OTFormatUserMessageHtml(category, id)
{
	return umFormatUserMessage(UM_HTML, category, id, arguments);
}

// --- Functions that write to document ---

// Writes result of OTGetUserMessage to the document
function OTMsgWrite(category, id)
{
	document.write(OTGetUserMessage(category, id));
}

// Writes result of OTFormatUserMessage to the document
function OTMsgFormat(category, id)
{
	document.write(umFormatUserMessage(UM_RAW, category, id, arguments));
}

// Writes result of OTFormatUserMessageText to the document
function OTMsgFormatText(category, id)
{
	document.write(umFormatUserMessage(UM_TEXT, category, id, arguments));
}

// Writes result of OTFormatUserMessageHtml to the document
function OTMsgFormatHtml(category, id)
{
	document.write(umFormatUserMessage(UM_HTML, category, id, arguments));
}

// --- Standard Formatting functions ---

// This is the official list of format functions. Do not use a builtin function
// without listing it here - when we implement this for another language, we need
// to know what filters we need to have available.

// Builtins:
//	Trim, LCase, UCase

function trim(s)
{
	var x = s.replace(/^\s+/i, "");
	x = x.replace(/\s+$/i, "");
	return x;
}

function ucase(s)
{
	return s.toUpperCase();
}

function ucase(s)
{
	return s.toLowerCase();
}

// Custom functions must not rely on the session. You can use it, just make
// sure it exists by doing an:
//	If Eval("IsObject(objSession)") = True Then
// then take some appropriate action when there is no session.

// Custom:



